package com.neusoft.SPNursingHome.controller;

import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/deepseek")
public class DeepSeekController {
    private final ChatModel chatModel;

    public DeepSeekController(ChatModel chatModel) {
        super();
        this.chatModel = chatModel;
    }

    @GetMapping("/chat")
    public String chat(String msg) {
        Prompt prompt=new Prompt(msg);
        String result= chatModel.call(prompt).getResult().getOutput().getText();
//      聊天模型的名称
        String model=chatModel.call(prompt).getMetadata().getModel();
//      消耗的token数量
        Integer token=chatModel.call(prompt).getMetadata().getUsage().getTotalTokens();
        System.out.println("聊天模型："+model);
        System.out.println("消耗token数量："+token);
        return result;
    }

    @GetMapping(value="/chatStream",produces = "text/event-stream;charset=utf-8")
    public Flux<String> chatStream(String msg){
        Prompt prompt=new Prompt(msg);
        // 聊天模型的名称
        String model=chatModel.call(prompt).getMetadata().getModel();
//      消耗的token数量
        Integer token=chatModel.call(prompt).getMetadata().getUsage().getTotalTokens();
        System.out.println("聊天模型："+model);
        System.out.println("消耗token数量："+token);
        return chatModel.stream(msg);
    }
}
